﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Goway.StackingUC.Forms
{
    /// <summary>
    /// 跑马灯消息
    /// </summary>
    public static class MarqueeMessage
    {
        static MarqueeMessage()
        {
            messageInterval = MessageInterval;
        }

        #region 跑马灯提示消息

        private static readonly int messageInterval;

        /// <summary>
        /// 跑马灯消息队列
        /// </summary>
        //private static readonly Queue<Message> MarqueeMessageQueue = new Queue<Message>();

        private static int MessageInterval => 5;

        //{
        //    get
        //    {
        //        int time = 5;
        //        if (active1 != null)
        //        {
        //            if (!int.TryParse(active1.ParamValue, out time))
        //            {
        //                time = 3;
        //            }
        //        }

        //        return time;
        //    }
        //}

        public static void ShowMessage(this FrmMain frm, string message)
        {
            ShowMessageFunc(frm, message);
        }

        private static void ShowMessageFunc(dynamic _this, string message)
        {
            if (_this.Created)                   // 页面未加载时不能显示信息
            {
                _this.BeginInvoke(new Action(() =>
                {
                    _this.GetMessageBox().Text = _this.GetMessageBox().Text.Insert(0, DateTime.Now.ToString("t") +
                        " : " + message + "\n");
                }));
            }
        }

        /// <summary>
        /// 主页面显示跑马灯消息
        /// </summary>
        /// <param name="message"></param>
        public static void ShowMarqueeMessage(this FrmMain frm, string message, bool isImmediately = false)
        {
            ShowMarqueeMessageFunc(frm, message, isImmediately);
        }

        /// <summary>
        /// 显示跑马灯消息方法
        /// </summary>
        /// <param name="_this"></param>
        /// <param name="message"></param>
        /// <param name="isImmediately"></param>
        private static void ShowMarqueeMessageFunc(dynamic _this, string message, bool isImmediately = false)
        {
            if (_this.Created)                   // 页面未加载时不能显示信息
            {
                _this.BeginInvoke(new Action(() =>
                {
                    Message _msg = new Message() { message = message };
                    {
                        _this.lblMessage.Text = _msg.message;
                        _msg.isShowed = true;
                        if (isImmediately)
                        {
                            _this.timerMessage.Interval = 1; // 跑马灯提醒、默认值5秒之后自动消失、 具体消失根据配置决定
                        }
                        else
                        {
                            _this.timerMessage.Interval = messageInterval * 1000; // 跑马灯提醒、默认值5秒之后自动消失、 具体消失根据配置决定
                        }

                        _this.timerMessage.Enabled = true;
                    }
                }));
            }
        }

        /// <summary>
        /// 跑马灯消息排队间隔时间显示
        /// </summary>
        /// <param name="_this"></param>
        public static void ShowMarqueeMessage_Tick(dynamic _this)
        {
            if (_this.Created) // 页面未加载时不能显示信息
            {
                _this.BeginInvoke(new Action(() =>
                {
                    //--暂时注释掉排队显示，待对齐后完善哪些消息排队显示，哪些消息立即显示
                    //if (MarqueeMessageQueue != null && MarqueeMessageQueue.Any())
                    {
                        /*            Message _msg;
                                    do
                                    {
                                        if (MarqueeMessageQueue == null || !MarqueeMessageQueue.Any())
                                        {
                                            return;
                                        }

                                        _msg = MarqueeMessageQueue.Dequeue(); // 跑马灯消息出队列
                                    } while (_msg.isShowed);

                                    _this.lblMessage.Text = _msg.message;*/
                    }
                    //else
                    {
                        _this.lblMessage.Text = "";
                        _this.timerMessage.Enabled = false;
                    }
                }));
            }
        }

        #endregion
    }

    /// <summary>
    /// 跑马灯消息体
    /// </summary>
    public class Message
    {
        /// <summary>
        /// 消息内容
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 是否已显示完成-默认未显示过
        /// </summary>
        public bool isShowed { get; set; }
    }
}
